import time
import sys
import os
from typing import List

# 添加当前目录到路径，确保能导入模块
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

try:
    from gen import generate_test_data, generate_edge_cases, generate_large_test_case
    from alg import Solution
except ImportError as e:
    print(f"导入错误: {e}")
    print("请确保gen.py和alg.py文件在同一目录下")
    sys.exit(1)

def test_five_cases():
    """测试5个核心用例"""
    print("=== 5个核心测试用例 ===")
    solution = Solution()
    
    # 精选5个最重要的测试用例
    test_cases = [
        ([5,5,5,10,20], True, "示例1: 正常找零"),
        ([5,5,10,10,20], False, "示例2: 无法找零"),
        ([5], True, "单个5美元"),
        ([10], False, "单个10美元(无法找零)"),
        ([], True, "空数组")
    ]
    
    all_passed = True
    for i, (bills, expected, description) in enumerate(test_cases):
        try:
            result = solution.lemonadeChange(bills)
            passed = result == expected
            all_passed &= passed
            
            print(f"测试用例{i+1}: {description}")
            print(f"   输入: {bills}")
            print(f"   期望结果: {expected}, 实际结果: {result}")
            print(f"   状态: {'✓ 通过' if passed else '✗ 失败'}")
            
            # 显示详细过程
            if hasattr(solution, 'lemonadeChangeWithDetails') and bills:
                success, process = solution.lemonadeChangeWithDetails(bills)
                for step in process[:3]:  # 只显示前3步
                    print(f"   {step}")
                if len(process) > 3:
                    print(f"   ... (共{len(process)}步)")
            print()
        except Exception as e:
            print(f"测试用例 {i+1} 执行失败: {e}")
            all_passed = False
    
    print(f"测试结果: {'✓ 全部通过' if all_passed else '✗ 存在失败'}")
    return all_passed

def test_performance():
    """简单性能测试"""
    print("=== 性能测试 ===")
    solution = Solution()
    
    try:
        # 生成一个适中规模的测试数据
        bills = generate_large_test_case(1000)
        
        if not bills:
            print("生成性能测试数据失败")
            return
        
        # 测试时间
        start_time = time.time()
        result = solution.lemonadeChange(bills)
        end_time = time.time()
        
        execution_time = (end_time - start_time) * 1000  # 转换为毫秒
        
        print(f"数据规模: 1000")
        print(f"执行时间: {execution_time:.2f}ms")
        print(f"结果: {result}")
        print(f"平均每个元素处理时间: {execution_time/1000:.4f}ms")
        print("✓ 性能测试完成")
        
    except Exception as e:
        print(f"性能测试失败: {e}")

def main():
    """主函数"""
    print("柠檬水找零问题 - 简化测试程序")
    print("=" * 50)
    
    try:
        # 运行5个核心测试用例
        success = test_five_cases()
        
        # 运行性能测试
        test_performance()
        
        if success:
            print("🎉 所有测试通过!")
        else:
            print("❌ 测试存在失败")
        
    except Exception as e:
        print(f"测试程序执行过程中发生错误: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    main()
